﻿using PID.Demo.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PID.Demo
{
    public partial class FrmConfig : Form
    {
        private readonly ConfigControl _config;
        private readonly string _path;
        public FrmConfig(string title, ConfigControl pid)
        {
            InitializeComponent();

            this.Text = "设置---" + title;

            comboBox1.SelectedIndex = 0;
            _config = pid;
            _path = Path.Combine(Application.StartupPath, "config.txt");

            this.checkBox1.Checked = _config._pid.OpenSaturation;
            numericUpDown1.Value = (decimal)_config._pid.MaxLim;
            numericUpDown2.Value = (decimal)_config._pid.MinLim;

            this.checkBox2.Checked = _config._pid.OpenDeadZone;
            numericUpDown4.Value = (decimal)_config._pid.DeadZoneMaxLim;
            numericUpDown3.Value = (decimal)_config._pid.DeadZoneMinLim;

            this.checkBox3.Checked = _config._pid.OpenInterfere;
            numericUpDown6.Value = (decimal)_config._pid.Alpha;

            this.checkBox4.Checked = _config._pid.OpenInput;
            numericUpDown7.Value = (decimal)_config._pid.InputUpperLimit;
            numericUpDown5.Value = (decimal)_config._pid.InputLowerLimit;

            this.checkBox5.Checked = _config._pid.OpenIntegral;
            numericUpDown9.Value = (decimal)_config._pid.IntegralSeparationFactor;

            numericUpDown8.Value= (decimal)_config.InitialValue;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // 输出抗饱和
            _config._pid.OpenSaturation = this.checkBox1.Checked;
            _config._pid.MaxLim = (float)numericUpDown1.Value;
            _config._pid.MinLim = (float)numericUpDown2.Value;
            // 死区
            _config._pid.OpenDeadZone = this.checkBox2.Checked;
            _config._pid.DeadZoneMaxLim = (float)numericUpDown4.Value;
            _config._pid.DeadZoneMinLim = (float)numericUpDown3.Value;
            // 微分抗干扰
            _config._pid.OpenInterfere = this.checkBox3.Checked;
            _config._pid.Alpha = (float)numericUpDown6.Value;
            // 输入抗饱和
            _config._pid.OpenInput = this.checkBox4.Checked;
            _config._pid.InputUpperLimit = (float)numericUpDown7.Value;
            _config._pid.InputLowerLimit = (float)numericUpDown5.Value;
            // 积分分离
            _config._pid.OpenIntegral = this.checkBox5.Checked;
            _config._pid.IntegralSeparationFactor = (float)numericUpDown9.Value;

            // 初始值
            _config.InitialValue = (float)numericUpDown8.Value;

            var dic = new Dictionary<string, object>();
            dic.Add("checkBox1", _config._pid.OpenSaturation);
            dic.Add("numericUpDown1", _config._pid.MaxLim);
            dic.Add("numericUpDown2", _config._pid.MinLim);

            dic.Add("checkBox2", _config._pid.OpenDeadZone);
            dic.Add("numericUpDown4", _config._pid.DeadZoneMaxLim);
            dic.Add("numericUpDown3", _config._pid.DeadZoneMinLim);

            dic.Add("checkBox3", _config._pid.OpenInterfere);
            dic.Add("numericUpDown6", _config._pid.Alpha);

            dic.Add("checkBox4", _config._pid.OpenInput);
            dic.Add("numericUpDown7", _config._pid.InputUpperLimit);
            dic.Add("numericUpDown5", _config._pid.InputLowerLimit);

            dic.Add("checkBox5", _config._pid.OpenIntegral);
            dic.Add("numericUpDown9", _config._pid.IntegralSeparationFactor);

            dic.Add("numericUpDown8", _config.InitialValue);
            ParameterHelper.SaveParameters(_path, dic);
            this.Close();
        }
    }
}
